package model.ds;

import model.HexDirection;
import model.PlayerColor;
import model.ds.piece.Piece;


public class GameTile implements GameTileInterface{

	private Piece[] pieces = new Piece[5];
	public GameTile upper;
	public GameTile upperright;
	public GameTile upperleft;
	public GameTile lower;
	public GameTile lowerright;
	public GameTile lowerleft;
	
	
	
	
	/**
	 * Adds a new Piece to this GameTile if this is not already occupied. 
	 * However, if piece is a bug we can still add it to this GameTile.
	 * @param piece
	 */
	public void add(Piece piece) {
		if(!isOccupied() || (isOccupied() && piece.getType()==PieceType.Bug)){
			int i;
			for(i=0;i<5;i++){
				if(pieces[i]==null)break;
			}
			pieces[i]=piece;	
		}
	
	}

	/**
	 * Returns the neighbouring GameTile in the HexDirection dir.
	 * @param dir
	 */
	public GameTile getNeighbor(HexDirection dir) {
		if(dir==HexDirection.Upper)return upper;
		if(dir==HexDirection.UpperRight)return upperright;
		if(dir==HexDirection.UpperLeft)return upperleft;
		if(dir==HexDirection.Lower)return lower;
		if(dir==HexDirection.LowerRight)return lowerright;
		if(dir==HexDirection.LowerLeft)return lowerleft;
		return null;		
	}
	
	//PA6
	/**
	 * Checks whether leaving this GameTile in the HexDirection dir would be a connected move. 
	 * This is true if one of the neighbouring HexDirections is occupied.
	 * @param dir
	 * @return
	 */
	public boolean isConnectedMove(HexDirection dir) {
		GameTile neighbour=getNeighbor(dir); 
		if((neighbour.isOccupied(HexDirection.Upper))
				|| (neighbour.isOccupied(HexDirection.UpperRight))
				|| (neighbour.isOccupied(HexDirection.UpperLeft))
				|| (neighbour.isOccupied(HexDirection.Lower))
				|| (neighbour.isOccupied(HexDirection.LowerRight))
				|| (neighbour.isOccupied(HexDirection.LowerLeft))) return true;
				
		return false;
	}

    /**
     * Returns true if this GameTile is occupied by a Piece.
     */
	public boolean isOccupied() {
		return pieces[0]!=null;
	}

	/**
	 * Returns true if the neighbouring GameTile in the HexDirection dir is occupied by a Piece.
	 * @param hexDirection
	 */
	public boolean isOccupied(HexDirection dir) {
		if(dir==HexDirection.Upper)return upper.isOccupied();
		if(dir==HexDirection.UpperRight)return upperright.isOccupied();
		if(dir==HexDirection.UpperLeft)return upperleft.isOccupied();
		if(dir==HexDirection.Lower)return lower.isOccupied();
		if(dir==HexDirection.LowerRight)return lowerright.isOccupied();
		if(dir==HexDirection.LowerLeft)return lowerleft.isOccupied();
		
		return false;
	}
	
	/**
	 public boolean isSurrounded(){
	 	HexDirection dir = HexDirection.UpperLeft;
		if(this.wqueen==-1)
		for(int i=0;i<6;i++){
			if(!isOccupied(dir.next()))return false;	
		}
		return true;
	}*/
	//PA6
	/**
	 * Checks whether the GameTile in the HexDirecton dir is passable by the top-most Piece of this GameTile. 
	 * The GameTile in HexDirection dir is passable if it is not occupied and the entrance to the GameTile is not blocked such 
	 * that a regular Piece cannot move through. (This method is obviously only reasonable for pieces which can move further than just one GameTile.)
	 * @param dir
	 */
	public boolean isPassable(HexDirection dir) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * Removes the top-most game Piece. GameTile becomes unoccupied if the last Piece is removed.
	 */
	public void remove() {
		int i;
		for(i=4;i>-1;i--){
			if(pieces[i]!=null)break;
		}
		
		pieces[i]=null;
		
	}
	/**GameTileInterface
	 * Puts a game Piece to an empty field.
	 * @param piece
	 */
	public void setPiece(Piece piece) {
		pieces[0]=piece;
		
	}
	
	public PlayerColor getColor(){
		int i;
		for(i=0;i<5;i++){
			if(pieces[i]==null)break;
		}
		return pieces[i-1].getColor();
	}
	
	public String toString(){
		int i;
		for(i=4;i>-1;i--){
			if(pieces[i]!=null)break;
		}
		
		return "test";//pieces[i];
		
	}
	
	

}
